package cn.tinkling.views;

import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.wb.swt.SWTResourceManager;

import cn.tinkling.HanziToPinyin;
import cn.tinkling.TinklingResourceManager;

/**
 * This sample class demonstrates how to plug-in a new workbench view. The view
 * shows data obtained from the model. The sample creates a dummy model on the
 * fly, but a real implementation would connect to the model available either in
 * this or another plug-in (e.g. the workspace). The view is connected to the
 * model using a content provider.
 * <p>
 * The view uses a label provider to define how model objects should be
 * presented in the view. Each view can present the same model objects using
 * different labels and icons, if needed. Alternatively, a single label provider
 * can be shared between views in order to ensure that objects of the same type
 * are presented in the same way everywhere.
 * <p>
 */

public class Pinyin extends ViewPart {

	/**
	 * The ID of the view as specified by the extension.
	 */
	public static final String ID = "cn.tinkling.views.Pinyin";

	StyledText srcText;

	private HanziToPinyin mHanziToPinyin;

	/**
	 * The constructor.
	 */
	public Pinyin() {
		mHanziToPinyin = new HanziToPinyin();
	}

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		parent.setBackground(TinklingResourceManager.getDefaultBackground());
		GridLayout gl_parent = new GridLayout(1, false);
		gl_parent.verticalSpacing = 8;
		parent.setLayout(gl_parent);

		srcText = new StyledText(parent, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		srcText.setAlwaysShowScrollBars(false);
		srcText.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		srcText.setForeground(SWTResourceManager.getColor(51, 51, 51));
		srcText.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 11, SWT.NORMAL));
		srcText.setRightMargin(2);
		srcText.setTopMargin(2);
		srcText.setBottomMargin(2);
		srcText.setLeftMargin(2);
		srcText.setWordWrap(true);
		GridData gd_srcText = new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1);
		gd_srcText.heightHint = 100;
		srcText.setLayoutData(gd_srcText);

		Button btnConvert = new Button(parent, SWT.NONE);
		btnConvert.setForeground(SWTResourceManager.getColor(SWT.COLOR_WIDGET_BORDER));
		btnConvert.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 12, SWT.NORMAL));

		GridData gd_btnConvert = new GridData(SWT.CENTER, SWT.CENTER, false, false, 1, 1);
		gd_btnConvert.widthHint = 135;
		btnConvert.setLayoutData(gd_btnConvert);
		btnConvert.setText("\u8F6C    \u6362");

		final StyledText resultText = new StyledText(parent, SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		resultText.setAlwaysShowScrollBars(false);
		resultText.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		resultText.setForeground(SWTResourceManager.getColor(51, 51, 51));
		resultText.setLeftMargin(2);
		resultText.setBottomMargin(2);
		resultText.setRightMargin(2);
		resultText.setTopMargin(2);
		resultText.setFont(SWTResourceManager.getFont("Microsoft YaHei UI", 11, SWT.NORMAL));
		resultText.setWordWrap(true);
		resultText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

		btnConvert.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String src = srcText.getText();
				String result = mHanziToPinyin.getPinyin(src);
				resultText.setText(result.toLowerCase());
			}
		});

		Menu resultMenu = new Menu(resultText);
		resultText.setMenu(resultMenu);

		MenuItem menuItemCopy = new MenuItem(resultMenu, SWT.NONE);
		menuItemCopy.setText("\u590D\u5236");
		menuItemCopy.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (resultText.getSelectionCount() > 0) {
					resultText.copy();
				} else if (resultText.getText().length() > 0) {
					StringSelection stsel = new StringSelection(resultText.getText());
					Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stsel, stsel);
				}
			}
		});

		Menu srcMenu = new Menu(srcText);
		srcText.setMenu(srcMenu);

		MenuItem menuItemPaste = new MenuItem(srcMenu, SWT.NONE);
		menuItemPaste.setText("\u7C98\u8D34");
		menuItemPaste.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				srcText.setText("");
				srcText.paste();
			}
		});
		MenuItem srcMenuItemCopy = new MenuItem(srcMenu, SWT.NONE);
		srcMenuItemCopy.setText("\u590D\u5236");
		srcMenuItemCopy.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (srcText.getSelectionCount() > 0) {
					srcText.copy();
				} else if (srcText.getText().length() > 0) {
					StringSelection stsel = new StringSelection(srcText.getText());
					Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stsel, stsel);
				}
			}
		});
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		srcText.setFocus();
	}
}